package chooongg.base

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.multidex.MultiDex
import chooongg.base.manager.AppManager
import java.util.*

abstract class BaseApplication : Application() {

    //打开的Activity数量统计
    private var activityStartCount = 0

    val activityStack = LinkedList<Activity>()

    val activityTop: Activity
        get() = activityStack.last


    val activityPrevious: Activity?
        get() = if (activityStack.size < 2) null else activityStack[activityStack.size - 2]

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)

    }

    override fun onCreate() {
        super.onCreate()
        activityLifecycle()
        AppManager.init(this)
        Utils.initialize(this)
    }

    private fun activityLifecycle() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                activityStack.addLast(activity)
            }

            override fun onActivityStarted(activity: Activity) {
                activityStartCount++
                if (activityStartCount == 1) {
                }
            }

            override fun onActivityResumed(activity: Activity) {
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
                activityStartCount--
                if (activityStartCount == 0) {
                }
            }

            override fun onActivityDestroyed(activity: Activity) {
                activityStack.remove(activity)
            }

            override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
            }
        })
    }

}
